

#ifndef __MUTEX_HPP__
#define __MUTEX_HPP__

#include <pthread.h>

class Mutex_Base
{
public:
    Mutex_Base(const Mutex_Base&) = delete;   // 不允许进行拷贝和赋值
    const Mutex_Base& operator=(const Mutex_Base& ) = delete;

    Mutex_Base()
    {
        pthread_mutex_init(&mutex_ , 0);
    }

    void Lock()
    {
        pthread_mutex_lock(&mutex_);
    }

    void Unlock()
    {
        pthread_mutex_unlock(&mutex_);
    }

    ~Mutex_Base()  
    {
        pthread_mutex_destroy(&mutex_);
    }
    
    pthread_mutex_t& GetMutexOriginal()   // 返回原生锁,为条件变量使用
    {
        return mutex_;
    }
private:
    pthread_mutex_t mutex_;
};

// 可以再进行一层封装,使用RAII的方式使用锁
class Mutex
{
public:
    Mutex(Mutex_Base& mutex)
    :mutex_(mutex)
    {
        mutex_.Lock();
    }
    ~Mutex()
    {
        mutex_.Unlock();
    }
private:
    Mutex_Base& mutex_;
};


#endif

